<?php
/**
 *
 * @author ninteySeven.
 * @date 2023-03-26 11:41
 */

namespace App\Services;

use App\Enum\ArticlesEnum;
use App\Enum\CityEnableEnum;
use App\Enum\NormalEnum;
use App\Enum\RedisEnum;
use App\Models\ArticlesModel;
use App\Models\CityEnableModel;
use App\Models\CityGpsModel;
use Illuminate\Support\Facades\DB;

class PublikService extends BaseService
{

    /**
     * 返回捐赠排行
     * @param $num
     * @return array
     */
    public static function donationTopList($num): array
    {
        $newArray = [];
        $rows = DB::select("SELECT * FROM `sxcx_donation_toplist` a WHERE $num > (SELECT COUNT( * ) FROM `sxcx_donation_toplist` WHERE `type` = a.`type` AND top < a.top ) ORDER BY a.type, a.top ASC");

        if(!empty($rows))
        {
            foreach( $rows as $row)
                $newArray[$row->type][] = ['name'=>$row->name,'money'=>$row->money,'top'=>$row->top];
        }
        unset($row);
        unset ($rows);
        return $newArray;
    }
    public static function articleTopList($num)
    {
        return ArticlesModel::query()->where(['state'=>ArticlesEnum::STATUS_TOP,'category_id'=>ArticlesEnum::CATEGORY_ZIXUN])->orderBy('distribute_date','desc')->limit($num)->get(['id','subject','cover_img']);
    }
    public static function articleDetail($articleId)
    {
        return ArticlesModel::query()->find($articleId,['id','subject','cover_img','content','distribute_date']);
    }
    public static function _fetchAdcode($location,$onlyCode=true){
        return MapService::_fetchAdcode($location,$onlyCode);
    }

    /**
     * 返回开通的城市 ，如果是直辖市，则城市代码升级
     *
     * @return array
     */
    public static function getCityEnabled(): array
    {
        return CityEnableModel::query()->where(['status'=> NormalEnum::STATUS_NORMAL])->orderBy('city_pinyin')
            ->get(['city_code','city_name','city_lat','city_lng','city_pinyin'])->toArray();
    }

    /**
     * 获取 城市的区县
     * @param $cityCode
     * @return mixed
     */
    public static function getCityDistricts($cityCode){
        return CityEnableModel::query()->where(['status'=>NormalEnum::STATUS_NORMAL,'city_code'=>$cityCode])->value('districts');
    }
}
